<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:Request.php,2010/05/07 17:41:13 
 * 
 **************************************************************************/
 
 
 
/**
 * @file HttpRequest.php
 * @author huqingping
 * @date 2010/05/07 17:41:13
 * @version 1.0 
 * @brief 
 *  
 **/

final class HttpRequest
{
	var $inputs = array();
	var $cookies = array();
	var $files = array();
	var $headers = array();
	var $method = 'GET';
	var $host = '';
	var $userip = 'undefined';
	var $clientip = 'undefined';
	var $url = '';
	var $uri = '';
	var $serverEnvs = array();
	var $now = 0;
	var $isPrivate = false;
	var $userData = array();
	var $appId = 0;

	//输入输出格式和编码
	var $of = 'default';
	var $oe = 'GBK';
	var $if = 'default';
	var $ie = 'GBK';
	var $ep = false;


	private $app = null;

	function __construct(WebApp $app)
	{
		$this->app = $app;
		$this->now = time();
	}

	//获取原始的参数数据，不经过各种过滤处理的
	function getu($key,$default=null)
	{
		if (isset($_GET[$key])) {
			return $_GET[$key];
		}
		if (isset($_POST[$key])) {
			return $_POST[$key];
		}
		return $default;
	}

	function get($key,$default=null)
	{
		if (is_string($key)) {
			if (isset($this->inputs[$key])) {
				return $this->inputs[$key];
			}
			return $default;
		} else {
			$ret = array();
			foreach($key as $k) {
				if (isset($this->inputs[$k])) {
					$ret[] = $this->inputs[$k];
				} else {
					$ret[] = $default;
				}
			}
			return $ret;
		}
	}
	function set($key,$value)
	{
		$this->inputs[$key] = $value;
	}

	function getCookie($key,$default=null)
	{
		if (isset($this->cookies[$key])) {
			return $this->cookies[$key];
		}
		return $default;
	}

	//是否需要跳到错误页
	function needErrorPage()
	{
		//get请求html或者default输出，或者强制_e=1跳转
		if ($this->ep) {
			return true;
		}
		if ($this->method === 'GET' &&
			($this->of == 'html' || $this->of == 'default')) {
				return true;
			}
		return false;
	}
	function getHeader($key,$default=null)
	{
		$name = 'HTTP_'.strtoupper($key);
		if (isset($this->serverEnvs[$name])) {
			return $this->serverEnvs[$name];
		}
		return $default;
	}

	function getFile($key)
	{
		if (isset($this->files[$key])) {
			return $this->files[$key];
		}
		return null;
	}
}






/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */